package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.IAdSensitiveService;
import com.heima.model.admin.dtos.AdSensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveService extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements IAdSensitiveService {
    /**
     * 根据名称分页模糊查询敏感词列表
     * @param adSensitiveDTO
     * @return
     */
    @Override
    public ResponseResult list(AdSensitiveDTO adSensitiveDTO) {
        //校验参数
        if(adSensitiveDTO == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        adSensitiveDTO.checkParam();
        //查询敏感词列表
        IPage<AdSensitive> pageQuery = new Page<>(adSensitiveDTO.getPage(), adSensitiveDTO.getSize());
        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.lambdaQuery();
        if(StringUtils.isNotBlank(adSensitiveDTO.getSensitives())){
            queryWrapper.like(AdSensitive::getSensitives, adSensitiveDTO.getSensitives());
        }
        IPage<AdSensitive> pageResult = this.page(pageQuery, queryWrapper);
        //响应结果
        PageResponseResult pageResponseResult = new PageResponseResult(adSensitiveDTO.getPage(), adSensitiveDTO.getSize(), pageResult.getTotal());
        pageResponseResult.setData(pageResult.getRecords());
        return pageResponseResult;
    }

    /**
     * 新增敏感词信息
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //校验参数
        if(adSensitive == null || StringUtils.isBlank(adSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
            //敏感词不能重复
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if(count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "敏感词已存在");
        }
        //保存敏感词
        adSensitive.setCreatedTime(new Date());
        this.save(adSensitive);
        //响应结果
        return ResponseResult.okResult();
    }

    /**
     * 修改敏感词信息
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //校验参数
        if(adSensitive == null || adSensitive.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdSensitive oldSensitive = this.getById(adSensitive.getId());
        if(oldSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "敏感词信息不存在");
        }
            //敏感词不能重复
        if(StringUtils.isNotBlank(adSensitive.getSensitives()) && !oldSensitive.getSensitives().equals(adSensitive.getSensitives())){
            int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
            if(count > 0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "敏感词重复了");
            }
        }
        //修改敏感词数据
        this.updateById(adSensitive);
        //响应结果
        return ResponseResult.okResult();
    }

    /**
     * 删除敏感词信息
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        //校验参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdSensitive adSensitive = this.getById(id);
        if(adSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "敏感词信息不存在");
        }
        //删除敏感词信息
        this.removeById(id);
        //响应结果
        return ResponseResult.okResult();
    }
}
